package sctu.edu.java.year2016.team01.renxinpeng.day20160610.chapter12_1;

import sctu.edu.java.year2016.team02.wanggaowen.Pra.Fil;

import java.awt.*;
import java.io.*;
import javax.swing.*;

/**
 * Created by Zero on 2016/6/10.
 * 实现读取文件时出现一个表示进度的进度条，可使用ProgressMonitorInputStream。
 */
public class Read extends JFrame {

    public static void main(String[] args) throws Exception{


        JFrame jl = new JFrame("读取文件进度条");
        Container container=jl.getContentPane();
        container.setLayout(new FlowLayout());
        jl.setSize(100, 100);
        jl.setVisible(true);
        jl.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        File file=new File("D:/","study.txt");


            FileOutputStream out =new FileOutputStream(file);
            byte study[]="好好学习!".getBytes();
            out.write(study);
            out.close();



        ProgressMonitorInputStream monitor;
        monitor = new ProgressMonitorInputStream(null, "Loading ",
                new FileInputStream(file));


        ProgressMonitor progressMonitor = monitor.getProgressMonitor();
        int readUnit=2;
        int all=monitor.available();
        int readed=0;
        byte data[]=new byte[readUnit];

        while (monitor.available()>0){

            Thread.sleep(5);

            int in=monitor.read(data);
            readed+=in;
            float process=(float)readed/all*100;
            progressMonitor.setNote("archived" + process + "%");


            }

        }
    }


